package com.qf.voteconsumer.init;

import com.qf.voteconsumer.vote.entity.Candidate;
import com.qf.voteconsumer.vote.service.ICandidateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 自定义项目启动的初始化方法的类
 */
@Component
public class CustomInitializer implements ApplicationRunner {

    @Autowired
    ICandidateService candidateService;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //1. 把所有候选人的名字查出来
        List<Candidate> candidates = candidateService.list();

        //2. 写入Redis的set中
        String key = "vote:candidateset";
        for (Candidate candidate : candidates) {
            redisTemplate.opsForSet().add(key,candidate.getName());
        }

    }
}
